package org.Game;

import java.util.ArrayList;

import com.example.gameex2.SpriteAnimation;

import org.Game.Player;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import AI.BaseState;
import AI.observeTarget;

public class Enemy
{
	public SpriteAnimation m_Top;
	public SpriteAnimation m_Base;
	private double m_fBaseRotate = 0.0f;
	private double m_fTopRotate = 0.0f;
	public BaseState m_pCurrentState = null;
	
	public Vector2 m_lookVector;
	
	float m_StrikingPower;
	float m_DefensivePower;

	float m_AttackSpeed;	
	public float m_MovementSpeed;
	
	int m_BaseX;
	int m_BaseY;
	
	public Player m_Target = null;
	
	protected Rect m_BoundBox; 
	
	public void SetBitmap(Bitmap top, Bitmap base)
	{
		m_Top = new SpriteAnimation(top);
		m_Base = new SpriteAnimation(base);
	}
	
	public void InitTopSprite(int left, int top, int width, int height, int fps, int iFrame)
	{
		m_Top.InitSpriteData(left, top, width, height, fps, iFrame);
		
		InitialStatus();
	}
	
	public void InitBottomSprite(int left, int top, int width, int height, int fps, int iFrame)
	{
		m_Base.InitSpriteData(left, top, width, height, fps, iFrame);
		m_BoundBox = new Rect();
		m_BoundBox.set(0, 0, width, height);
	}
	
	public void SetPosition(int x, int y)
	{
		m_Top.SetPosition(x, y);
		m_Base.SetPosition(x, y);
		m_BaseX = x;
		m_BaseY = y;
		m_BoundBox.set(x, y, x + m_Base.GetBitmap().getWidth(), y + m_Base.GetBitmap().getHeight());
	}
	
	public void SetTarget(Player player)
	{
		m_Target = player;
	}
	
	public void Update(int MapX, int MapY) {
		// TODO Auto-generated method stub
		m_pCurrentState.Execute(this);
		long GameTime = System.currentTimeMillis();
		int x = m_BaseX + MapX;
		int y = m_BaseY + MapY;
		m_Top.SetPosition(x, y);
		m_Base.SetPosition(x, y);
		m_BoundBox.set(m_Base.GetX() - m_Base.GetBitmap().getWidth()/2 - 21, 
				m_Base.GetY() - m_Base.GetBitmap().getHeight()/2 -20, 
				m_Base.GetX() + m_Base.GetBitmap().getWidth()/2 + 20, 
				m_Base.GetY() + m_Base.GetBitmap().getHeight()/2 + 20);
		m_Top.Update(GameTime);
		m_Base.Update(GameTime);
	}

	public void Draw(Canvas canvas) {
		// TODO Auto-generated method stub
		m_Base.DrawRotate(canvas, m_fBaseRotate);
		m_Top.DrawRotate(canvas, m_fTopRotate);
	}
	
	public void MoveBase(int CenterX, int CenterY, float x, float y)
	{
		int CrrentX = CenterX;
		int CrrentY = CenterY;
		Vector2 direction = new Vector2(x - CrrentX, y - CrrentY);
		Vector2.Normalize(direction);
		m_lookVector = direction;
		m_fBaseRotate = Vector2.Dot(direction, new Vector2(1.0f, 0.0f));
		m_fBaseRotate = Math.toDegrees(Math.acos(m_fBaseRotate));
		if(y < CrrentY)
			m_fBaseRotate = -m_fBaseRotate;
	}
	
	public void RotateTop(float x, float y)
	{
		int CrrentX = m_Top.GetCenterX();
		int CrrentY = m_Top.GetCenterY();
		Vector2 direction = new Vector2(x - CrrentX, y - CrrentY);
		Vector2.Normalize(direction);
		m_fTopRotate = Vector2.Dot(direction, new Vector2(1.0f, 0.0f));
		m_fTopRotate = Math.toDegrees(Math.acos(m_fTopRotate));
		if(y < CrrentY)
			m_fTopRotate = -m_fTopRotate;
	}
	
	void InitialStatus()
	{
		m_StrikingPower = 1.0f;
		m_DefensivePower = 1.0f;
		m_AttackSpeed = 1.0f;	
		m_MovementSpeed = 0.1f;
		m_lookVector = new Vector2(1.0f, 0.0f);
		ChangeState(observeTarget.getInstance());
	}

	public void ChangeState(BaseState pNewState)
	{
		if(m_pCurrentState != null)
			m_pCurrentState.Exit(this);
		
		m_pCurrentState = pNewState;
		
		m_pCurrentState.Enter(this);
	}
}
